﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace OneMake.Model
{
    //全网各类目买家地域分析
    public class report_qw_area_stat
    {
        public List<string> brands { get; set; }
        public Dictionary<string, Dictionary<string, int>> values { get; set; }
        public report_qw_area_stat()
        {
            List<string> brands = new List<string>();
            values = new Dictionary<string,Dictionary<string,int>>();
        }
    }

    public class report_qw_area_stat2
    {
        public int shopid { get; set; }
        public int sum_amount { get; set; }
        public string province { get; set; }
        public string brand { get; set; }
    }

    partial class ReportHelper
    {
        List<string> get_report_qw_area_stat_brand(DateTime beginTime, DateTime endTime, int shopid)
        {
            var db = new PetaPoco.Database("mysql" + UserSession.Network);
            string sql = "select brand from mf_m_mjdyfx where stat_datetime BETWEEN @0 AND @1 AND shopid=@2 group by brand";
            IDataReader reader = db.ExecuteReader(sql, beginTime, endTime, shopid);

            List<string> titles = new List<string>();
            titles.Add("省份");
            while (reader.Read())
            {
                string title = reader["brand"].ToString();
                titles.Add(title);
            }
            return titles;
        }

        public report_qw_area_stat get_report_qw_area_stat(DateTime beginTime, DateTime endTime, int shopid)
        {
            var db = new PetaPoco.Database("mysql" + UserSession.Network);
            string sql = @"select shopid,stat_datetime,sum(amount) as sum_amount,province,brand 
                            from mf_m_mjdyfx 
                            where brand=@0 and shopid=@1 and stat_datetime BETWEEN @2 AND @3
                            group by province";

            report_qw_area_stat report = new report_qw_area_stat();
            report.brands = get_report_qw_area_stat_brand(beginTime, endTime, shopid);
            foreach (string brand in report.brands)
            {
                IEnumerable<report_qw_area_stat2> items = db.Query<report_qw_area_stat2>(sql, brand, shopid, beginTime, endTime);

                foreach(report_qw_area_stat2 item in items)
                {
                    if(!report.values.ContainsKey(item.province))
                        report.values.Add(item.province, new Dictionary<string, int>());

                    Dictionary<string,int> row = report.values[item.province];
                    row.Add(item.brand, item.sum_amount);
                }
            }

            return report;
        }
    }
}
